﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace BinocularVision
{
    // Based on: Binocular vision example (c) Mattias Wahde 2009, 2010
    public partial class MainForm : Form
    {
        #region Fields
        private Thread gUIThread;
        private double gUIThreadSleepInterval;
        private bool running;
        #endregion

        #region Constructors
        public MainForm()
        {
            InitializeComponent();
            this.startVideoViewButton.Enabled = false;
        }
        #endregion

        #region Private methods
        private void GUIUpdateLoop()
        {
            while (running)
            {
                leftWebCamPictureBox.Image = Robot.Instance.Body.LeftWebCam.ReadingAsBitmap;
                rightWebCamPictureBox.Image = Robot.Instance.Body.RightWebCam.ReadingAsBitmap;
                leftWebCamFaceDetectionPictureBox.Image = Robot.Instance.Brain.FaceDetection.LeftWebCamProcessedImage;
                rightWebcamFaceDetectionPictureBox.Image = Robot.Instance.Brain.FaceDetection.RightWebCamProcessedImage;
                Thread.Sleep((int)(gUIThreadSleepInterval * 1000));
            }
        }


        private void startButtonClick(object sender, EventArgs e)
        {
            Robot.Instance.Body = new Body();
            Robot.Instance.Body.SensorPollingThreadSleepInterval = 0.050; // s
            Robot.Instance.Body.LeftWebCam.WebCamDeviceNr = 0;
            Robot.Instance.Body.LeftWebCam.ImageWidth = 320;
            Robot.Instance.Body.LeftWebCam.ImageHeight = 240;
            Robot.Instance.Body.LeftWebCam.FrameRate = 10;
            Robot.Instance.Body.RightWebCam.WebCamDeviceNr = 1;
            Robot.Instance.Body.RightWebCam.ImageWidth = 320;
            Robot.Instance.Body.RightWebCam.ImageHeight = 240;
            Robot.Instance.Body.RightWebCam.FrameRate = 10;

            Robot.Instance.Brain = new Brain();
            Robot.Instance.Brain.BrainStepThreadSleepInterval = 0.010;
            Robot.Instance.Brain.UtilityFunctionUpdateInterval = 0.050;
            Robot.Instance.Brain.FaceDetection.BrainProcessThreadSleepTime = 0.040;
            Robot.Instance.Brain.FaceDetection.Utility = 0.5;

            Robot.Instance.Initialize();
            Robot.Instance.Start();

            this.startRobotButton.Enabled = false;
            this.startVideoViewButton.Enabled = true;
        }

        private void startVideoViewButton_Click(object sender, EventArgs e)
        {
            if (!running)
            {
                running = true;
                gUIThreadSleepInterval = 0.050;
                gUIThread = new Thread(GUIUpdateLoop);
                gUIThread.Start();
                startVideoViewButton.Text = "Stop video view";
            }
            else
            {
                running = false;
                gUIThread.Join();
                startVideoViewButton.Text = "Start video view";
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            leftWebCamPictureBox.Image = Robot.Instance.Body.LeftWebCam.ReadingAsBitmap;
            rightWebCamPictureBox.Image = Robot.Instance.Body.RightWebCam.ReadingAsBitmap;
        }

        private void button1_Click_2(object sender, EventArgs e)
        {
            Robot.Instance.Brain.FaceDetection.Step();
        }
        
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Robot.Instance != null)
            {
                Robot.Instance.Stop();
                Robot.Instance.Disconnect();
            }
            Environment.Exit(0);
        }
        #endregion       

        #region Properties
        #endregion
    }
}